package com.cmas.manage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cmas.common.core.domain.entity.SysDept;
import com.cmas.common.core.domain.entity.SysUser;
import com.cmas.manage.domain.ActUser;
import com.cmas.manage.domain.ParentInfo;
import com.cmas.manage.mapper.ActUserMapper;
import com.cmas.manage.mapper.ParentInfoMapper;
import com.cmas.manage.vo.StuStuDetail;
import com.cmas.system.mapper.SysDeptMapper;
import com.cmas.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.cmas.manage.bo.StudentInfoAddBo;
import com.cmas.manage.bo.StudentInfoQueryBo;
import com.cmas.manage.bo.StudentInfoEditBo;
import com.cmas.manage.domain.StudentInfo;
import com.cmas.manage.mapper.StudentInfoMapper;
import com.cmas.manage.vo.StudentInfoVo;
import com.cmas.manage.service.IStudentInfoService;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 学生信息Service业务层处理
 *
 * @author cmas
 * @date 2021-05-01
 */
@Service
public class StudentInfoServiceImpl extends ServiceImpl<StudentInfoMapper, StudentInfo> implements IStudentInfoService {

    @Autowired
    private StudentInfoMapper studentInfoMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private ParentInfoMapper parentInfoMapper;

    @Autowired
    private ActUserMapper actUserMapper;

    @Override
    public StudentInfoVo queryById(Long id) {
        StudentInfo db = this.baseMapper.selectById(id);
        return BeanUtil.toBean(db, StudentInfoVo.class);
    }

    @Override
    public List<StudentInfoVo> queryList(StudentInfoQueryBo bo) {
        LambdaQueryWrapper<StudentInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getAccount() != null, StudentInfo::getAccount, bo.getAccount());
        lqw.eq(StrUtil.isNotBlank(bo.getStuNo()), StudentInfo::getStuNo, bo.getStuNo());
        lqw.eq(StrUtil.isNotBlank(bo.getLocation()), StudentInfo::getLocation, bo.getLocation());
        lqw.eq(StrUtil.isNotBlank(bo.getNativePlace()), StudentInfo::getNativePlace, bo.getNativePlace());
        lqw.eq(StrUtil.isNotBlank(bo.getPolitical()), StudentInfo::getPolitical, bo.getPolitical());
        lqw.eq(bo.getAdmissionTime() != null, StudentInfo::getAdmissionTime, bo.getAdmissionTime());
        lqw.eq(bo.getBirthday() != null, StudentInfo::getBirthday, bo.getBirthday());
        lqw.eq(StrUtil.isNotBlank(bo.getStuName()), StudentInfo::getStuName, bo.getStuName());
        lqw.eq(StrUtil.isNotBlank(bo.getEmail()), StudentInfo::getEmail, bo.getEmail());
        lqw.eq(StrUtil.isNotBlank(bo.getSex()), StudentInfo::getSex, bo.getSex());
        lqw.eq(StrUtil.isNotBlank(bo.getPhonenumber()), StudentInfo::getPhonenumber, bo.getPhonenumber());
        lqw.eq(StrUtil.isNotBlank(bo.getAvatar()), StudentInfo::getAvatar, bo.getAvatar());
        lqw.eq(bo.getDeptId() != null, StudentInfo::getDeptId, bo.getDeptId());
        return entity2Vo(this.list(lqw));
    }


    @Override
    public List<StudentInfoVo> queryListVo(StudentInfo studentInfo) {
        return studentInfoMapper.selectListVo(studentInfo);
    }


    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<StudentInfoVo> entity2Vo(Collection<StudentInfo> collection) {
        List<StudentInfoVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, StudentInfoVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<StudentInfo> page = (Page<StudentInfo>) collection;
            Page<StudentInfoVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(StudentInfoAddBo bo) {
        StudentInfo add = BeanUtil.toBean(bo, StudentInfo.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(StudentInfoEditBo bo) {
        StudentInfo update = BeanUtil.toBean(bo, StudentInfo.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(StudentInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }

    @Override
    public StuStuDetail selectStuDetail(Long id) {

        StuStuDetail stuStuDetail = new StuStuDetail();
        // 学生信息
        StudentInfo studentInfo = studentInfoMapper.selectById(id);

        List<SysDept> sysDepts = sysDeptMapper.selectParentNode(studentInfo.getDeptId());
        for (SysDept sysDept : sysDepts) {
            if (StrUtil.equals(sysDept.getDeptType(), "1")) {
                studentInfo.setCollege(sysDept.getDeptName());
            } else if (StrUtil.equals(sysDept.getDeptType(), "2")) {
                studentInfo.setInstitute(sysDept.getDeptName());
            } else if (StrUtil.equals(sysDept.getDeptType(), "3")) {
                studentInfo.setMajor(sysDept.getDeptName());
            }else if (StrUtil.equals(sysDept.getDeptType(), "4")) {
                studentInfo.setStuClass(sysDept.getDeptName());
            }
        }
        stuStuDetail.setStudentInfo(studentInfo);
        // 父母信息
        ParentInfo parentInfo = parentInfoMapper.selectOne(new QueryWrapper<ParentInfo>().eq("stu_id", id));
        if (parentInfo != null) {
            stuStuDetail.setParentInfo(parentInfo);
        }
        // 参项信息
        List<ActUser> actUsers = actUserMapper.selectActInfo(id);
        if (actUsers.size() != 0) {
            stuStuDetail.setActUserList(actUsers);
        }
        return stuStuDetail;
    }


}
